package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

/**
 * 初始化管理员
 * ContextRefreshedEvent 开启容器后刷新
 */
@Service
public class AdminInitServiceImpl implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private ILoginInfoService loginInfoService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        //创建第一个管理员
        if (!loginInfoService.existUsername("admin")) {
            LoginInfo admin = new LoginInfo();
            admin.setNickName("管理员");
            admin.setUsername("admin");
            admin.setPassword(MD5.encode("000000admin"));
            admin.setUserType(LoginInfo.USERTYPE_MGRSITE);
            loginInfoService.insert(admin);
        }

    }
}
